package com.example.dllo.keepdemo.dynamic.find;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.dynamic.bean.FindBean;
import com.example.dllo.keepdemo.greendao.DynamicFind;
import com.example.dllo.keepdemo.tools.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/15.
 */
public class HeaderGridViewAdapter extends BaseAdapter {
    private Context context;
    private FindBean data;
    private List<DynamicFind> dynamicFindList;
    //test = 1为断网状态
    private int test;

    public HeaderGridViewAdapter(Context context,FindBean data,int hasNet) {
        this.context = context;
        this.data = data;
        this.test = hasNet;
    }

    public HeaderGridViewAdapter(List<DynamicFind> dynamicFindList, Context context,int noneNet) {
        this.dynamicFindList = dynamicFindList;
        this.context = context;
        this.test = noneNet;
    }

    @Override
    public int getCount() {
        if (test == 1) {
            return dynamicFindList.size();
        } else {
            return data.getData().size();
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_headergridview_fragment_find,parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (test == 1) {
            viewHolder.tvContent.setText(dynamicFindList.get(position).getContent());
            viewHolder.tvLikes.setText(dynamicFindList.get(position).getLikes() + "");
            viewHolder.tvComment.setText(dynamicFindList.get(position).getComment() + "");

            ImageLoader.ImageListener listener = ImageLoader.getImageListener(viewHolder.imageView,R.mipmap.placeholder41_41,R.mipmap.no_entryp_hoto);
            VolleySingleton.getImageLoader().get(dynamicFindList.get(position).getUrl(),listener,300,150);
        } else {
            viewHolder.tvContent.setText(data.getData().get(position).getContent());
            viewHolder.tvLikes.setText(data.getData().get(position).getLikes() + "");
            viewHolder.tvComment.setText(data.getData().get(position).getComments() + "");

            ImageLoader.ImageListener listener = ImageLoader.getImageListener(viewHolder.imageView,R.mipmap.placeholder41_41,R.mipmap.no_entryp_hoto);
            VolleySingleton.getImageLoader().get(data.getData().get(position).getPhoto(),listener,300,150);
        }


        return convertView;
    }

    class ViewHolder {
        private TextView tvContent,tvLikes,tvComment;
        private ImageView imageView;
        public ViewHolder(View view) {
            tvContent = (TextView) view.findViewById(R.id.tv_item_fragment_find_content);
            tvLikes = (TextView) view.findViewById(R.id.tv_item_fragment_find_likes);
            tvComment = (TextView) view.findViewById(R.id.tv_item_fragment_find_comment);
            imageView = (ImageView) view.findViewById(R.id.img_item_fragment_find_picture);
        }
    }
}
